package com.lanchetech.merchant.controller;

import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.request.ShopAmountReq;
import com.lanchetech.bean.request.TenantFeatureReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.CustomPageEditVO;
import com.lanchetech.bean.vo.ShopAmountVO;
import com.lanchetech.bean.vo.ShopCollectionVO;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.*;
import com.lanchetech.merchant.aop.OperatorLog;
import com.lanchetech.merchant.service.StoreService;
import com.lanchetech.service.KuaidizsService;
import com.raycloud.open.sdk.api.response.KdzsPrintTradeCreateResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Api(value = "店铺接口", tags = "店铺接口")
@RequestMapping("/api/store")
@RestController
public class StoreController {

    @Autowired
    StoreService storeService;

    @Autowired
    KuaidizsService kuaidizsService;


    @ApiOperation("获取门店详情")
    @GetMapping("/getShopDetail")
    public ResultData<Shop> getShopDetail(@AuthenticationPrincipal Merchant merchant) {
        return storeService.getShopDetail(merchant);
    }

    @ApiOperation("获取店铺合约")
    @PreAuthorize("hasAuthority('store.contract')")
    @GetMapping("/getContract")
    public ResultData<Tenant> getContract(@AuthenticationPrincipal Merchant merchant) {
        return storeService.getContract(merchant);
    }

    @OperatorLog("编辑店铺合约")
    @PreAuthorize("hasAuthority('store.contract')")
    @ApiOperation("编辑店铺合约")
    @PostMapping("/editContract/{step}")
    public ResultData<Long> editContract(@RequestBody Tenant tenant, @PathVariable("step") Integer step) {
        return storeService.editContract(tenant, step);
    }

    @OperatorLog("编辑店铺描述信息")
    @ApiOperation("编辑店铺描述信息")
    @PreAuthorize("hasAuthority('store.store-setting-description')")
    @PostMapping("/editTenantDescription")
    public BaseResp editTenantDescription(@AuthenticationPrincipal Merchant merchant, @RequestBody Tenant tenant) {
        return storeService.editTenantDescription(merchant, tenant);
    }

    @OperatorLog("编辑店铺背景图")
    @ApiOperation("编辑店铺背景图")
    @PreAuthorize("hasAuthority('store.store-setting-image')")
    @PostMapping("/editTenantImageUrl")
    public BaseResp editTenantImageUrl(@AuthenticationPrincipal Merchant merchant, @RequestBody Tenant tenant) {
        return storeService.editTenantImageUrl(merchant, tenant);
    }

    @OperatorLog("编辑店铺推荐商品")
    @ApiOperation("编辑店铺推荐商品")
    @PreAuthorize("hasAuthority('store.store-setting-recommend')")
    @PostMapping("/editRecommendSpu")
    public BaseResp editStoreRecommendSpu(@AuthenticationPrincipal Merchant merchant, @RequestBody List<Long> spuIds) {
        return storeService.editStoreRecommendSpu(merchant, spuIds);
    }

    @OperatorLog("编辑店铺属性")
    @ApiOperation("编辑店铺属性")
    @PreAuthorize("hasAnyAuthority('store.store-setting-store-new','store.store-setting-display','store.store-setting-store-activity', 'store.store-setting-store-recommend')")
    @PostMapping("/editTenantFeature")
    public BaseResp editTenantFeature(@AuthenticationPrincipal Merchant merchant, @RequestBody TenantFeatureReq req) {
        return storeService.editTenantFeature(merchant, req);
    }

    @ApiOperation("获取店铺推荐商品列表")
    @PreAuthorize("hasAuthority('store.store-setting-recommend')")
    @GetMapping("/getRecommendSpuList")
    public ResultData<List<Spu>> getRecommendSpuList(@AuthenticationPrincipal Merchant merchant) {
        return storeService.getRecommendSpuList(merchant);
    }

    @OperatorLog("编辑自定义专题")
    @ApiOperation(value = "编辑自定义专题")
    @PreAuthorize("hasAuthority('store.custom-page.edit.auth')")
    @PostMapping("/editCustomPage")
    public BaseResp editCustomPage(@RequestBody CustomPageEditVO req) {
        return storeService.editCustomPage(req);
    }

    @ApiOperation(value = "获取自定义专题详情")
    @GetMapping("/getCustomPageDetail")
    public ResultData<CustomPageEditVO> getCustomPageDetail(@RequestParam("id") Long id) {
        return storeService.getCustomPageDetail(id);
    }

    @OperatorLog("删除自定义专题")
    @ApiOperation(value = "删除自定义专题")
    @PreAuthorize("hasAuthority('store.custom-page.delete.auth')")
    @GetMapping("/deleteCustomPage")
    public BaseResp deleteCustomPage(@RequestParam("id") Long id) {
        return storeService.deleteCustomPage(id);
    }

    @ApiOperation(value = "获取自定义专题分页")
    @PreAuthorize("hasAuthority('store.custom-page')")
    @PostMapping("/getCustomPage")
    public ResultData<BasePageResp<CustomPage>> getCustomPage(@RequestBody PageReq req) {
        return storeService.getCustomPage(req);
    }

    @ApiOperation("获取所有店铺设置")
    @PreAuthorize("hasAuthority('store.tenant-config')")
    @GetMapping("/getAllTenantConfig")
    public ResultData<List<TenantConfig>> getAllTenantConfig() {
        return storeService.getAllTenantConfig();
    }

    @OperatorLog("编辑店铺配置")
    @ApiOperation("编辑店铺配置")
    @PreAuthorize("hasAuthority('store.tenant-config.edit.auth')")
    @PostMapping("/editTenantConfig")
    public BaseResp editTenantConfig(@RequestBody TenantConfig req) {
        return storeService.editTenantConfig(req);
    }

    @OperatorLog("删除店铺配置")
    @ApiOperation(value = "删除店铺配置")
    @PreAuthorize("hasAuthority('store.tenant-config.edit.auth')")
    @GetMapping("/deleteTenantConfig")
    public BaseResp deleteTenantConfig(@RequestParam("id") Long id) {
        return storeService.deleteTenantConfig(id);
    }

    @ApiOperation("获取店铺业绩排名分页")
    @PostMapping("/getShopAmountPage")
    public ResultData<BasePageResp<ShopAmountVO>> getShopAmountPage(@RequestBody ShopAmountReq req) {
        return storeService.getShopAmountPage(req);
    }


    @ApiOperation("修改店铺logo")
    @PostMapping("/editShopAvatar")
    public BaseResp editShopAvatar(@RequestBody Shop req) {
        return storeService.editShopAvatar(req);
    }

    @ApiOperation("修改店铺名称")
    @PostMapping("/editShopNickname")
    public BaseResp editShopNickname(@RequestBody Shop req) {
        return storeService.editShopNickname(req);
    }

    @ApiOperation("修改店铺售后电话")
    @PostMapping("/editShopAfterSalePhone")
    public BaseResp editShopAfterSalePhone(@RequestBody Shop req) {
        return storeService.editShopAfterSalePhone(req);
    }

    @ApiOperation("修改店铺售后地址")
    @PostMapping("/editShopAfterSaleAddress")
    public BaseResp editShopAfterSaleAddress(@RequestBody Shop req) {
        return storeService.editShopAfterSaleAddress(req);
    }

    @ApiOperation("修改店铺退货地址")
    @PostMapping("/editShopReturnAddress")
    public BaseResp editShopReturnAddress(@RequestBody Shop req) {
        return storeService.editShopReturnAddress(req);
    }

    @ApiOperation("获取免登陆地址")
    @GetMapping("/getLoginUrl")
    public String getLoginUrl() {
        return kuaidizsService.getLoginUrl();
    }

    @ApiOperation("测试提交订单")
    @GetMapping("/testCreateTrade")
    public KdzsPrintTradeCreateResponse testCreateTrade(@RequestParam("id") Long id) {
        return kuaidizsService.testCreateTrade(id);
    }

    @ApiOperation("测试修改订单")
    @GetMapping("/testUpdateTrade")
    public Boolean testUpdateTrade(@RequestParam("id") Long id) {
        return kuaidizsService.testUpdateTrade(id);
    }

    @ApiOperation("测试修改订单退款状态")
    @GetMapping("/testUpdateTradeRefund")
    public Boolean testUpdateTradeRefund(@RequestParam("id") Long id) {
        return kuaidizsService.testUpdateTradeRefund(id);
    }

    @ApiOperation("获取店铺粉丝分页")
    @PostMapping("/getShopCollectionPage")
    public ResultData<BasePageResp<ShopCollectionVO>> getShopCollectionPage(@RequestBody PageReq req) {
        return storeService.getShopCollectionPage(req);
    }

    @ApiOperation("设置店铺代理身份")
    @PreAuthorize("hasAuthority('store.tenant-config.setStoreIdentity.auth')")
    @PostMapping("/setStoreIdentity")
    public BaseResp setStoreIdentity(@RequestParam Long userId) {
        BaseResp baseResp = new BaseResp();
        try {
            storeService.setStoreIdentity(userId);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @ApiOperation("设置店铺代理拿货折扣")
    @PreAuthorize("hasAuthority('store.tenant-config.agencyDiscount.auth')")
    @PostMapping("/agencyDiscount")
    public BaseResp agencyDiscount(@RequestParam Long userId,
                                   @RequestParam BigDecimal discount) {
        BaseResp baseResp = new BaseResp();
        try {
            storeService.agencyDiscount(userId, discount);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @ApiOperation("修改店铺简介")
    @PostMapping("/editShopIntroduction")
    public BaseResp editShopIntroduction(@RequestBody CompanyIntroduction req) {
        return storeService.editShopIntroduction(req);
    }

    @ApiOperation("获取店铺简介")
    @GetMapping("/getShopIntroduction")
    public ResultData<CompanyIntroduction> getShopIntroduction() {
        return storeService.getShopIntroduction();
    }

    @ApiOperation("修改店铺自动回复模版")
    @GetMapping("/editAutoReplyFormat")
    public BaseResp editAutoReplyFormat(@RequestParam("replyFormat") String replyFormat) {
        return storeService.editAutoReplyFormat(replyFormat);
    }

}
